package com.bfs.anasit.jsfbean;

import com.bfs.anasit.model.Patient;
import com.bfs.anasit.model.Record;
import com.bfs.anasit.service.PatientService;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: lukman
 * Date: 6/20/11
 * Time: 12:23 PM
 * To change this template use File | Settings | File Templates.
 */
@Named
@RequestScoped
public class PatientBean implements Serializable {
    @Inject
    private static Logger log;

    @Inject
    private Patient patient;


    private List<Patient> patientList;

    @Inject
    private Record record;

    @Inject
    private PatientService patientService;

    private Patient selectedPatient;

    public PatientBean() {
    }

    public Record getRecord() {
        return record;
    }

    public void setRecord(Record record) {
        this.record = record;
    }


    public PatientService getPatientService() {
        return patientService;
    }

    public void setPatientService(PatientService patientService) {
        this.patientService = patientService;
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    @Produces
    public List<Patient> getPatientList() {
        patientList = patientService.findAllPatients();
        return patientList;
    }

    public void setPatientList(List<Patient> patientList) {
        this.patientList = patientList;
    }

    public String createPatient() {
        patientService.save(patient);
        return "/patient/patients?faces-redirect=true";
    }

    public void clearPatientProperties() {
        patient.setPatientId(null);
        patient.getName().setFirstName("");
        patient.getName().setMiddleName("");
        patient.getName().setLastName("");

    }

    public Patient getSelectedPatient() {
        return selectedPatient;
    }

    public void setSelectedPatient(Patient selectedPatient) {
        this.selectedPatient = selectedPatient;
    }

    public void onEditRow(RowEditEvent event) {
        Patient selectedPatient = (Patient) event.getObject();
        patientService.save(selectedPatient);
    }

    public String onRowSelectEditPatient(SelectEvent sevt) {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedPatient", sevt.getObject());

        return "update?faces-redirect=true";

    }
}
